<?php 
	//1. 连接数据库，并判断
	require "../public/functions.php";
		$link = mysqli_connect("localhost","root","") or die("数据库连接失败！");
	
	//2. 设置字符集
		mysqli_set_charset($link,"utf8");
		
	//3. 选择数据库
		mysqli_select_db($link,"bbs");

	//根据动作执行相应的操作
	switch($_GET['a']){
		case "addFather":

			//将父类名称写入数据库
			$father = $_POST['father'];
			if(!empty($father)){
				//4.将父类名称添加到数据库当中
				$sql = "insert into type (name) values ('{$father}')";
				$result = mysqli_query($link,$sql);

				//5.判断是否添加成功
				if($result && mysqli_affected_rows($link)>0){

					//告诉用户，添加成功
					echo "<script>alert('父类名称添加成功！');window.location.href='show.php'</script>";

				}
//			var_dump($_POST);
			}else {
				echo "<script>alert('父类名称不能为空！');window.location.href='add.php'</script>";

			}
			

		break;
		
		case "addChild":	//执行子类添加
		
			//获取子类名称，父类id
			$fid = $_POST['fid'];
			$child = $_POST['child'];
			$blogo = $_FILES['file'];
//			var_dump($blogo);

			$path="../../public/home/img/";
			$maxsize=0;
			$fileType=array("image/jpeg","image/gif","image/png");
			$res=upload($path,$fileType,$maxSize=0,$blogo);
//			var_dump($res);
			$newName = $res['newName'];

			$line = "x_";
			$maxw =55;
			$maxh =55;
			getImages($path,$newName,$line,$maxw,$maxh);

//    $path = rtrim($path,'/').'/';
			$newblogo = $line.$newName;
			if(!empty($child) && !empty($newblogo)){
				//将这两个内容写到数据库当中
				$sql = "insert into type (name,pid,path,blogo) values ('{$child}',{$fid},'0-{$fid}','{$newblogo}')";
				$result = mysqli_query($link,$sql);

				//判断
				if($result && mysqli_affected_rows($link)>0){

					echo "<script>alert('子类名称和logo添加成功！');window.location.href='main_menu.php'</script>";

				}
			}else{
				echo "<script>alert('子类名称和logo都不能为空！');window.location.href='show.php'</script>";

			}

		break;
	}

?>